package com.informationsystem.model;

import static javax.persistence.GenerationType.IDENTITY;

import com.informationsystem.model.BaseObject;

import org.hibernate.annotations.OrderBy;
import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.IndexedEmbedded;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MappedSuperclass;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.print.attribute.standard.DateTimeAtCompleted;

import javax.xml.bind.annotation.XmlRootElement;

import java.io.Serializable;

@MappedSuperclass
@Indexed
@XmlRootElement
public class Information extends BaseObject implements Serializable {
    public Long id;
    private Systemuser systemuser;
    private String title;
    private Date postDate = new Date();
    private Long viewTimes = (long) 0;

    @Id  @GeneratedValue(strategy=IDENTITY) @DocumentId    
    @OrderBy(clause = "id asc")
    public Long getId() {
        return this.id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }
@ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="user_id", nullable=false)
    public Systemuser getSystemuser() {
        return this.systemuser;
    }
    
    public void setSystemuser(Systemuser systemuser) {
        this.systemuser = systemuser;
    }
    
    @Column(name="title", length=100)
    @Field
    public String getTitle() {
        return this.title;
    }
    
    public void setTitle(String title) {
        this.title = title;
    }
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="post_date", nullable=false, length=19)
    @Field
    public Date getPostDate() {
        return this.postDate;
    }
    
    public void setPostDate(Date postDate) {
        this.postDate = postDate;
    }
    
    @Column(name="view_times", nullable=false)
    @Field
    public Long getViewTimes() {
        return this.viewTimes;
    }
    
    public void setViewTimes(Long viewTimes) {
        this.viewTimes = viewTimes;
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Information pojo = (Information) o;

        if (systemuser != null ? !systemuser.equals(pojo.systemuser) : pojo.systemuser != null) return false;
        if (title != null ? !title.equals(pojo.title) : pojo.title != null) return false;
        if (postDate != null ? !postDate.equals(pojo.postDate) : pojo.postDate != null) return false;
        if (viewTimes != null ? !viewTimes.equals(pojo.viewTimes) : pojo.viewTimes != null) return false;

        return true;
    }

    public int hashCode() {
        int result = 0;
        result = (systemuser != null ? systemuser.hashCode() : 0);
        result = 31 * result + (title != null ? title.hashCode() : 0);
        result = 31 * result + (postDate != null ? postDate.hashCode() : 0);
        result = 31 * result + (viewTimes != null ? viewTimes.hashCode() : 0);

        return result;
    }

    public String toString() {
        StringBuffer sb = new StringBuffer(getClass().getSimpleName());

        sb.append(" [");
        sb.append("id").append("='").append(getId()).append("', ");
        sb.append("systemuser").append("='").append(getSystemuser()).append("', ");
        sb.append("title").append("='").append(getTitle()).append("', ");
        sb.append("postDate").append("='").append(getPostDate()).append("', ");
        sb.append("viewTimes").append("='").append(getViewTimes()).append("', ");
        
        
        
        
        
        sb.append("]");
      
        return sb.toString();
    }
    
	@Transient
	public String getDisplayPostDate(){
		SimpleDateFormat dt = new SimpleDateFormat("MM-dd-yyyy"); 
		return dt.format(postDate);
	}   

}
